package com.android.test.model;

import java.util.ArrayList;
import com.android.test.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class OrgSearchResultAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<OrgDetailInfo> mData;
	private ArrayList<Boolean> mExplandStatus;
	public OrgSearchResultAdapter(Context con,ArrayList<OrgDetailInfo> list) {
		mInflater = LayoutInflater.from(con);
		mData = list;
		mExplandStatus = new ArrayList<Boolean>();
		initExpandedState();
	}

	public void initExpandedState(){
		if(mData!=null&&mData.size()>0){
			mExplandStatus.clear();
			for(int i = 0;i<mData.size();i++){
				mExplandStatus.add(new Boolean(false));
			}
		}
	}

	@Override
	public int getCount() {
		return null==mData?0:mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final OrgDetailInfo item = 	mData.get(position);
		final ViewHolder holder;
		if (convertView == null
				|| !(convertView.getTag() instanceof ViewHolder)) {
		    convertView = mInflater.inflate(R.layout.item_org_search_result,
			    parent, false);
		    holder = new ViewHolder();
		    holder.name = (TextView) convertView.findViewById(R.id.org_name);
		    holder.detail = convertView.findViewById(R.id.org_detail);
		    holder.city = (TextView) convertView.findViewById(R.id.org_city);
		    holder.address = (TextView) convertView.findViewById(R.id.org_address);
		    holder.phone = (TextView) convertView.findViewById(R.id.org_phone);
		    holder.postcode = (TextView) convertView.findViewById(R.id.org_postcode);
		    holder.gtcode = (TextView)convertView.findViewById(R.id.org_gtcode);
		    convertView.setTag(holder);
		} else {
		    holder = (ViewHolder) convertView.getTag();
		}
		// 设置ListView背景颜色
//		if (position % 2 != 0) {
//		    convertView.setBackgroundResource(R.drawable.list_selector_grey);
//		} else {
//		    convertView.setBackgroundResource(R.drawable.list_selector_white);
//		}
		if(item!=null){
			holder.name.setText(item.shortName);
			holder.detail.setVisibility(mExplandStatus.get(position) ? View.VISIBLE: View.GONE);
			holder.city.setText("城市："+item.mCity);
			holder.address.setText("地址："+item.mainAddress);
			holder.phone.setText("电话："+item.telephoneNum);
			holder.postcode.setText("邮编："+item.mPostCode);
			holder.gtcode.setText("经度："+item.mGpoint.getLongitudeE6()+", 纬度："+item.mGpoint.getLatitudeE6());
		}
		return convertView;
	}

	public void toggle(int position) {
		mExplandStatus.set(position, !mExplandStatus.get(position));
		notifyDataSetChanged();
	}
	
	class ViewHolder{
		TextView name;
		View detail;
		TextView city;
		TextView address;
		TextView phone;
		TextView postcode;
		TextView gtcode;
	}
}
